<?php

namespace app\model\v1\recovery;

use constants\ProductConstants;
use crmeb\traits\ModelTrait;
use think\Exception;
use think\exception\ValidateException;
use think\Model;

class RecoveryFeedBackItem extends Model
{
    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];


    protected $table = 'eb_recovery_feedback_item';






    /**
     * 个人中心 - 常见问题 - 获取问题详情页面
     * @param $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getFeedBackItem($params = [])
    {
        $item = self::whereNull("delete_time")->where("f_id", $params["f_id"])
            ->where("status", 1)
            ->select()
            ->toArray();

        return $item;
    }





    public static function getQuestionList($param = [])
    {
        $order = ["a.id"=>"desc"];

        $data["data"] = self::alias("a")
            ->field ("a.id,a.content,a.list_item,a.status,a.f_id,FROM_UNIXTIME(a.create_time) as create_time,b.list_item as cate_name")
            ->join ('recovery_feedback b','a.f_id=b.id','left')
            ->where(function ($query) use ($param){
                $query->whereNull("a.delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('a.create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("a.list_item",'like', '%'.$param["word"].'%');
                if(!empty($param["f_id"])) $query->where("a.f_id", $param["f_id"]);
            })
            ->order($order)
            ->page($param['page'], $param['limit'])
            ->select()->toArray();

        $data["count"] = self::alias("a")
            ->field ("a.id,a.content,a.list_item,a.status,a.f_id,a.create_time,b.list_item as cate_name")
            ->join ('recovery_feedback b','a.f_id=b.id','left')
            ->where(function ($query) use ($param){
                $query->whereNull("a.delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('a.create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("a.list_item",'like', '%'.$param["word"].'%');
                if(!empty($param["f_id"])) $query->where("a.f_id", $param["f_id"]);
            })
            ->count();

        return $data;
    }




    public static function addFeedbackItem($param = [])
    {
        $add = [
            "content"       => $param["content"],  //问题标题
            "list_item"     => $param["list_item"] ?? '', //回答文本
            "f_id"          => $param["f_id"],  //分类id
            "create_time"   => time(),
        ];
        return self::insert($add);
    }


    /**
     *
     * @param $param
     * @return RecoveryFeedBackItem
     */
    public static function updateFeedbackItem($param = [])
    {
        $update["update_time"] = time();  //更新时间
        if(!empty($param["list_item"]))     $update["list_item"] = $param["list_item"];  //回答文本
        if(!empty($param["content"]))       $update["content"] = $param["content"];  //问题标题
        if(!empty($param["f_id"]))          $update["f_id"] = $param["f_id"]; //分类id

        return self::where("id", $param["id"])->update($update);
    }




    public static function enableFeedbackItem($param = [])
    {
        //获取用户发送的id，根据状态修改当前banner的启用禁用状态
        $status = self::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }



}